package com.example.arithmeticleetcode.leetcode.September;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-09-08 09:42
 **/
public class Demo07 {

    public static void main(String[] args) {

        System.out.println(findKthBit(3, 1)); //0
        System.out.println(findKthBit(4, 11)); // 1
        System.out.println(findKthBit(1, 1)); //0
        System.out.println(findKthBit(2, 3)); // 1

    }

    public static char findKthBit(int n, int k) {
        List<String> list = new ArrayList<>();

        list.add("0");
        for (int i = 1; i < n; i++) {
            String s = list.get(i - 1);
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = s.length() - 1; j >= 0; j--) {
                if (s.charAt(j) == '0') {
                    stringBuilder.append("1");
                } else {
                    stringBuilder.append("0");
                }
            }
            list.add(s + "1" + stringBuilder.toString());
        }
        System.out.println(list);
        return list.get(n - 1).charAt(k - 1);
    }
}
